package ch04.Map;

/**
 * @author chenmin
 * @desc TODO
 * @date 2025/9/4
 */
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Map2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一组字符串：");
        String input = scanner.nextLine();

        // TreeMap 会按照 key 的自然顺序（字母顺序）排序
        Map<Character, Integer> charCountMap = new TreeMap<>();

        for (char c : input.toCharArray()) {
            if (Character.isLetter(c)) {
                charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
            }
        }

        System.out.println("按字母排序后每个字母出现的次数：");
        for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }

        scanner.close();
    }
}
